<?php
/**
 * house service
 */
namespace App\Service;

use App\Models\EwHouseSeco;
use App\Service\CatsService;
use App\Util\Fun;

class HouseSecoService  extends BaseService
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	//地图等级
	const MAP_LEVEL_REGION = 'region';//区域等级
	const MAP_LEVEL_CITY   = 'city';//城市等级
	const MAP_LEVEL_NAME   = 'house';//详细等级

	/**
	 * [getHouseList 长链接获取房源列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getHouseList($params)
	{
        $sql_city ='';
        if($params->page_num < 1000){
            $sql_city = 'h.city_id = ' . $params->city_id .' AND';
        }
		$params->city_id = property_exists($params,'city_id') ? $params->city_id : 63;
		
        
		$sql = 'SELECT h.id,h.id AS go_url, 8 AS groupId,h.three_url,1 AS sum, " ' . self::MAP_LEVEL_NAME . ' " AS map_level,

		h.thumb,h.name,h.region,h.city_id,h.city_id AS city,
		h.layout,h.area,h.house_orien,h.house_name,
		"ershoufang" AS house_type,
		IFNULL(h.house_tags,"") AS house_tags,
		h.total_price,h.price,h.sources,h.visit,h.lon,h.lat,h.is_self,
		0 AS is_collect,0 AS is_praise,0 AS index_by,0 AS reco_by,
		h.property,h.id AS word,h.comment,h.create_time

		FROM ew_house_seco AS h
		WHERE '.$sql_city.' is_display = 1 AND is_del = 0 AND id <=(SELECT id FROM ew_house_seco ORDER BY id DESC limit '.($params->p-1)*$params->page_num.',1) ORDER BY id DESC LIMIT ' . $params->page_num;
        //($params->p-1)*$params->page_num
        
		$data = (new Fun)->db_execute($sql);
		return $data;
	}

	/**
	 * [getVillageList 长链接通过小区名称获取房源列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getVillageList($params)
	{
		$params->city_id = property_exists($params,'city_id') ? $params->city_id : 63;
		$params->region_id = property_exists($params,'region_id') ? $params->region_id : 0;
		$params->house_name = property_exists($params,'house_name') ? $params->house_name : "";

		$sql = 'SELECT h.id,h.id AS go_url, 8 AS groupId,h.three_url,1 AS sum, "' . self::MAP_LEVEL_NAME . '" AS map_level,

		h.thumb,h.name,h.region,h.city_id,h.city_id AS city,
		h.layout,h.area,h.house_orien,h.house_name,
		"ershoufang" AS house_type,
		IFNULL(h.house_tags,"") AS house_tags,
		h.total_price,h.price,h.sources,h.visit,h.lon,h.lat,h.is_self,
		0 AS is_collect,0 AS is_praise,0 AS index_by,0 AS reco_by,
		h.property,h.id AS word,h.comment,h.create_time

		FROM ew_house_seco AS h
		WHERE city_id = ' . $params->city_id . '  AND is_display = 1 AND is_del = 0
		AND village = "' . $params->house_name . '"
		ORDER BY id DESC
		LIMIT ' . ($params->p-1)*$params->page_num . ',' . $params->page_num;

		$data = (new Fun)->db_execute($sql);
		return $data;
	}

	/**
	 * [getList 房源列表 APP端列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$map_level = self::MAP_LEVEL_NAME;
		$table_id = self::getTableId();
		//默认获取类型第一条 二手房
		// $arr = CatsService::getTableCats($table_id);
		// $cate_id = current($arr) ? $arr[0]['id'] : NULL;
		// $params->cate_id = (isset($params->cate_id) && ($params->cate_id)) ? $params->cate_id : $cate_id;

		// if (isset($params->is_reco) && $params->is_reco)//获取最新推荐的房源 类型不做筛选
		// 	$params->cate_id = NULL;

		$member_id = property_exists($params,'member_id') ? $params->member_id : 0;
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		//已取消列表地图获取 2017-09-14
		/*$groupBy = 'h.id';
		//is_map 获取 城市地图
		if (isset($params->is_map) && $params->is_map){
			$groupBy = 'h.region';$params->page_num = 50; $map_level = self::MAP_LEVEL_REGION;
		}

		//region 通过区域名称 获取区域内的小区 地图
		if (isset($params->region) && $params->region){
			$groupBy = 'h.id';$params->page_num = 200; $map_level = self::MAP_LEVEL_NAME;
		}

		//百度地图level > 7 缩放>2km 展示区内房源套数
		if (isset($params->zoom_level) && $params->zoom_level && intval($params->zoom_level) > 7){
			$groupBy = 'h.region';$params->page_num = 50;
		}

		$lon = NULL; $lon_max = NULL; $lon_min = NULL; $lat = NULL; $lat_max = NULL; $lat_min = NULL;
		//百度地图level < 8 缩放<2km 展示区
		if (isset($params->zoom_level) && $params->zoom_level && intval($params->zoom_level) < 8){
			$groupBy = 'h.id';$params->page_num = 1000;

			if (isset($params->location) && $params->location && is_array($params->location) && count($params->location) == 2){
				$lon = $params->location[0]; $lat = $params->location[1]; $lon_max = $lon + 0.1; $lon_min = $lon - 0.1; $lat_max = $lat + 0.1; $lat_min = $lat - 0.1; $params->page_num = 1000;
			}
		}
		*/
		$builder->columns(
			'h.id,h.id as go_url,' . self::getTableId() . ' as groupId,h.three_url, count(h.id) as sum, "' . $map_level . '" AS map_level,
			h.thumb,h.name,h.region,h.city_id,h.city_id as city,
			h.layout,h.area,h.house_orien,h.house_name,
			"ershoufang" AS house_type,
			IFNULL(h.house_tags,"") AS house_tags,
			h.total_price,h.price,
			h.sources,h.visit,
			h.lon,h.lat,
			h.is_self,
			0 AS is_collect,0 AS is_praise,
			IFNULL(h.index_by,0) AS index_by,IFNULL(h.reco_by,0) AS reco_by,
			h.property,h.id as word,
			h.comment,h.create_time'
			);

		if ($count)
			$builder->columns('h.id');

		$builder->addFrom("App\Models\EwHouseSeco", 'h');
		$builder->andWhere("h.is_del = " . self::DEL_FALSE);

		//is_display
		if (isset($params->is_display))
			$builder->andWhere("h.is_display = ". $params->is_display);

		//target_id 获取相关推荐列表
		if (isset($params->target_id) && $params->target_id){
			$arr = new \stdClass();
			$arr->table_id = self::getTableId();
			$arr->list_id  = $params->target_id;
			$data = RecoService::getRecoList($arr);
			if (count($data) > 0){
				$builder->inWhere('h.id', $data);
			}
		}

		// //通过经纬度查询 （百度地图中心点经纬度获取+-0.1内的房源）
		// if ($lon_max && $lon_min && $lat_max && $lat_min)
		// 	$builder->andwhere('h.lon >= ' . $lon_min . ' AND h.lon <= ' . $lon_max . ' AND h.lat >= ' . $lat_min . ' AND h.lat <= ' .$lat_max);
		
		//title 
		if (isset($params->title) && $params->title)
			$builder->andwhere('h.name LIKE "%'.$params->title.'%"');

		//tags 
		if (isset($params->tags) && $params->tags)
			$builder->andWhere("h.tags = ". $params->tags);

		//city_id
		if (isset($params->city_id) && $params->city_id)
			$builder->andWhere("h.city_id = ". $params->city_id);

		//cate_id
		// if (isset($params->cate_id) && $params->cate_id)
		// 	$builder->andWhere("h.cate_id = ". $params->cate_id);

		//is_map 地图
		if (isset($params->is_map) && $params->is_map)
			$builder->andWhere('h.lon > 0 AND h.lat > 0');

		//region 通过区域名称 获取区域内的小区 地图
		if (isset($params->region) && $params->region)
			$builder->andWhere('h.region LIKE "%' . $params->region . '%"');

		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		//第一版app 首页-新房  新房-首页
		/*// $sql = " h.app_level DESC,h.order_by DESC, h.create_time DESC, h.id DESC, h.update_time DESC";
		//框架无法使用case when 
		// $sql = " h.index_by DESC, h.order_by DESC, h.create_time DESC, h.id DESC, h.update_time DESC";//默认获取 首页 - house
		// if (isset($params->is_reco) && $params->is_reco)//获取推荐 - house
		// 	$sql = " h.reco_by DESC, h.order_by DESC, h.create_time DESC, h.id DESC, h.update_time DESC";
		
		//order_by 排序
		if (isset($params->order_by) && $params->order_by)
			$sql = parent::orderBy($params->order_by, $table = "h");

		$builder->orderBy($sql);
		$builder->groupBy($groupBy);*/

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();		
	}

	/**
	 * [getSearch 房源搜索 后台端列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getSearch($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['h.id,h.three_url,h.thumb,h.name,h.layout,h.area,h.total_price,h.price,h.sources,h.visit,h.comment,h.address,h.is_display,h.order_by,h.reco_by,h.is_reco_you,h.create_time']);
		
		if ($count)
			$builder->columns('h.id');

		$builder->addFrom("App\Models\EwHouseSeco", 'h');
		$builder->andWhere("h.is_del = " . self::DEL_FALSE);

		//position_id 通过position_id 获取后台弹出列表  1 首页
		if (isset($params->position_id) && $params->position_id == 2) //推荐房源
			$builder->andWhere("h.reco_by  != '2' AND h.index_by != '2' AND h.is_display = 1");
		if (isset($params->position_id) && $params->position_id == 1) //房源首页
			$builder->andWhere("h.reco_by  != '2' AND h.index_by != '2' AND h.is_display = 1");

		//reco_by
		if (isset($params->reco_by))//首页banner列表 reco_by = 2
			$builder->andWhere("h.reco_by = '" . $params->reco_by . "'");

		//index_by
		if (isset($params->index_by))//首页视频列表 index_by = 2
			$builder->andWhere("h.index_by = '" . $params->index_by . "'");

		//is_display
		if (isset($params->is_display))
			$builder->andWhere("h.is_display = ". $params->is_display);

		//city_id
		if (isset($params->city_id) && $params->city_id)
			$builder->andWhere("h.city_id = ". $params->city_id);

		// if (isset($params->cate_id) && $params->cate_id)
		// 	$builder->andWhere("h.cate_id = ". $params->cate_id);

		//name 名称
		if (isset($params->name) && $params->name)
			$builder->andwhere('h.name LIKE "%'.$params->name.'%"');

		//region 区域
		if (isset($params->region) && $params->region)
			$builder->andwhere('h.region LIKE "%'.$params->region.'%"');

		//min_price 价格
		if (isset($params->min_price) && $params->min_price)
			$builder->andwhere('h.price > ' . $params->min_price);

		//max_price 价格
		if (isset($params->max_price) && $params->max_price)
			$builder->andwhere('h.price < ' . $params->max_price);

		//layout 户型
		if (isset($params->layout) && $params->layout)
			$builder->andwhere('h.layout LIKE "%'.$params->layout.'%"');

		//address 地址
		if (isset($params->address) && $params->address)
			$builder->andwhere('h.address LIKE "%'.$params->address.'%"');

		//sources 来源
		if (isset($params->sources) && $params->sources)
			$builder->andwhere('h.sources LIKE "%'.$params->sources.'%"');

		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		$sql = " h.order_by DESC, h.create_time DESC, h.id DESC, h.update_time DESC";
		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by, $table = "h");

		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}

	/**
	 * [getSubs 获取订阅推荐房源]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getSubs($params)
	{
		$params->p = 1;
		$params->page_num = 10;

		$memberId = property_exists($params,'member_id') ? $params->member_id : 0;
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(self::getTableId() . ' as groupId,
			IFNULL((SELECT count(c.id) FROM App\Models\EwMemberCollect c where c.list_id = h.id AND c.table_id = ' . self::getTableId() . ' AND c.member_id = ' . $memberId . '),0) AS is_collect,
			h.id,h.id as go_url,h.name,h.tags,(SELECT c.name AS cate_name FROM App\Models\EwCats c where h.cate_id = c.id) AS cate_name,h.thumb,h.price,h.region,h.layout,h.sources,h.visit,h.comment');
		$builder->addFrom("App\Models\EwHouse", 'h');
		$builder->andWhere("h.is_del = " . self::DEL_FALSE);

		//cate_id 房源类型
		if (isset($params->cate_id) && $params->cate_id >= 0)
			$builder->andwhere('h.cate_id = '. $params->cate_id);

		//price
		if (isset($params->price) && $params->price && $params->price != "价格不限" && $params->price != "不限"){

			if (mb_substr($params->price,-2,2,"UTF-8") == "以下"){

				$builder->andwhere('h.price <= '.intval($params->price));

			}else if (mb_substr($params->price,-2,2,"UTF-8") == "以上"){

				$builder->andwhere('h.price >= '.intval($params->price));

			}else{

				$price = explode("-", $params->price);
				if(count($price) >= 2){
					$builder->andwhere('h.price between ' .intval($price[0]). ' and '. intval($price[1]));
				}
			}
		}

		//area
		/*if (isset($params->area) && $params->area && $params->area != "面积不限" && $params->area != "不限"){

			if (mb_substr($params->area,-2,2,"UTF-8") == "以下"){

				$builder->andwhere('h.area <= '.intval($params->area));

			}else if (mb_substr($params->area,-2,2,"UTF-8") == "以上"){

				$builder->andwhere('h.area >= '.intval($params->area));

			}else{

				$area = explode("-", $params->area);
				if(count($area)>=2){
					$builder->andwhere('h.area between ' .intval($area[0]). ' and '. intval($area[1]));
				}
			}
		}*/

		//region
		// if (isset($params->region) && $params->region){
		// 	$region = explode(",", $params->region);
		// 	for ($i=0; $i <count($region) ; $i++) { 
		// 		if ($i == 0){
		// 			$sql = 'h.region LIKE "%' . $region[$i] .'%"'; 
		// 		}else{
		// 			$sql .= ' OR h.region LIKE "%' . $region[$i] .'%"'; 
		// 		}				
		// 	}
		// 	$builder->andwhere($sql);
		// }

		//layout
		if (isset($params->layout) && $params->layout && $params->layout != "户型不限" && $params->layout != "不限"){
			$sql = ' 1 = 1 ';
			$layout = explode(",", $params->layout);
			$map = [
				'一' => '1',
				'二' => '2',
				'三' => '3',
				'四' => '4',
				'五' => '5',
				'六' => '6',
				'七' => '7',
				'八' => '8',
				'九' => '9',
			];
			for ($i=0; $i <count($layout) ; $i++) { 
				if ($i == 0){
					$sql .= ' AND h.layout LIKE "%' . $layout[$i] .'%"';  //一室，二室，三室，四室
				}else{
					$sql .= ' AND h.layout LIKE "%' . $layout[$i] .'%"'; 
				}				
			}

			$arr = [];
			foreach ($layout as $key => $value) {
				$arr[] = str_replace(array_keys($map), array_values($map), $value);
			}
			for ($i=0; $i <count($arr) ; $i++) { 
				if ($i == 0){
					if (intval($arr[$i]) >= 5){
						$sql .= 'OR layout LIKE "%别墅%"'; 
					}else{
						$sql .= 'OR h.layout LIKE "%' . intval($arr[$i]) .'%"'; //1室 2室 3室 4室 5室
					}
				}else{
					if (intval($arr[$i]) >= 5){//
						$sql .= ' OR h.layout LIKE "%别墅%"'; 
					}else{
						$sql .= ' OR h.layout LIKE "%' . intval($arr[$i]) .'%"'; 
					}
				}				
			}

			$builder->andwhere($sql);
		}

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$builder->orderBy($sql);
		}

		//分页
		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}

	
	
	/**
	 * [getTableName 获取数据表名称]
	 * @return [type] [description]
	 */
	public static function getTableName()
	{
		$model = new EwHouseSeco;
		return $model->getSource();
	}

	/**
	 * [getSchema 获取数据库名称]
	 * @return [type] [description]
	 */
	public static function getSchema()
	{
		$model = new EwHouseSeco;
		return $model->getSchema();
	}

	/**
	 * [getTableId 获取数据表Id]
	 * @return [type] [description]
	 */
	public static function getTableId()
	{
		return 8;
		// return ConfigsService::getTable(self::getTableName());
	}

	public static function getModel($id = 0)
	{
		return EwHouseSeco::findFirst($id);
	}

	/**
	 * [getRecoInfo 通过id查询数据 相关推荐]
	 * @param  [type] $reco_ids [description]
	 * @return [type]           [description]
	 */
	public static function getRecoInfo($id)
	{

		$arr = new \stdClass();
		$arr->table_id = self::getTableId();
		$arr->list_id  = $id;
		$data = RecoService::getRecoList($arr);
		if(empty($data)) return $data;
		$query = EwHouseSeco::query();
		// $query->columns(['id,name,thumb,price,region,layout,address,sources']);
		$query->columns(['id,name,thumb,price,region,sources']);
		$query->inWhere('id', $data);
		
		$data = $query->execute();
		if ($data){
			return $data;
		}
	}

	/**
	 * [getInfo 通过主键获取房源详情  APP端 点赞量及当前登录用户是否点赞]
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public static function getInfo($id = 0, $member_id = NULL)
	{	
		$info = EwHouseSeco::findFirst($id);
		if (!$info)
			return NULL;
		$info = $info->toArray();

		$data = new \stdClass();
		$data->go_url  = "";
		$data->groupId = self::getTableId();
		$data->praise = (string)0;
		$data->is_praise = (string)-1;

		$arr = new \stdClass();
		$arr->table_id = self::getTableId();
		$arr->list_id  = $id;
		$arr->praise   = NULL;
		//获取点赞量
		$data->praise = (string)MemberPraiseService::sum($arr);
		//获取当前用户是否点赞
		if ($member_id){
			$arr->member_id = $member_id;
			$data->is_praise = (string)MemberPraiseService::juge($arr);
		}
		
		foreach ($info as $key => $value) {
			$data->$key = $value;
		}
		
		return $data;
	}

	/**
	 * [count 获取总量]
	 * @return [type] [description]
	 */
	public static function count()
	{
		// $sql = 'SELECT COUNT(*) AS counts FROM ew_house_seco';
		$sql = 'SELECT val FROM ew_configs WHERE key_name = "house_seco_count"';
		$data = (new Fun)->db_execute($sql);
		return $data[0]->val;
		// return EwHouseSeco::count();
	}

	/**
	 * [getStick 置顶]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getStick($id)
	{
		if (isset($id) && $id)
			$raw = EwHouseSeco::findFirst($id);

		if ($raw){
			$max = EwHouseSeco::maximum(['column' => 'order_by']);
			$raw->order_by = $max + 1;
			return $raw->save();
		}
		return NULL;
	}
	
	/**
	 * [visit 浏览量 +1]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function visit($id)
	{
		if (isset($id) && $id)
			$data = EwHouseSeco::findFirst($id);

		if ($data){
			$data->visit = $data->visit + 1;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [display 上架]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function display($id)
	{
		if (isset($id) && $id)
			$data = EwHouseSeco::findFirst($id);

		if ($data){
			$data->is_display = $data->is_display ? self::DISPLAY_FALSE : self::DISPLAY_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 移除/恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = EwHouseSeco::findFirst($id);

		if ($data){
			$data->is_del = $data->is_del ? self::DEL_FALSE : self::DEL_TRUE;
			return $data->save();
		}

		return NULL;		
	}

	/**
	 * [delete 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwHouseSeco::findFirst($id);

		if ($row && $row->delete()){
			$arr = new \stdClass();
		 	$arr->table_id = self::getTableId();
			$arr->list_id  = $id;
			RecoService::deleteRecoList($arr);
			return TRUE;
		}

		return NULL;
	}

	/**
	 * [save 保存]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$data->price_bk = intval($data->price_bk) > 0 ? intval($data->price_bk) : intval($data->price);

		$row = new EwHouseSeco;
		if (isset($data->id) && $data->id)
			$row = EwHouseSeco::findFirst($data->id);

		//置顶
		if (isset($data->is_stick) && $data->is_stick){
			$max = EwHouseSeco::maximum(['column' => 'order_by']);
			$data->order_by = $max + 1;
		}
		if (isset($data->layout)){
			// $str = "3室户型(5)，4室户型(1) 二室 三室 四室 别墅";
			// 三室 四室 二室 别墅 
			$cb_num  = ['/[\x{4e00}]室/u','/[\x{4e09}]室/u','/[\x{4e8c}]室/u','/[\x{56db}]室/u','/[\x{4e94}]室/u','/[\x{516d}]室/u','/[\x{4e03}]室/u','/[\x{516b}]室/u','/[\x{4e5d}]室/u','/[\x{5341}]室/u'];
			$replace = ['1室','2室','3室','4室','5室','6室','7室','8室','9室','10室'];
			$new_str = preg_replace($cb_num, $replace, $data->layout);
			preg_match_all('/([1-9]室|别墅)/', $new_str, $layout);
			$arr = array_unique($layout[0]);
			$da_num=array('零','一','二','三','四','五','六','七','八','九'); 
			$str = "";
			if (count($arr) > 0){
				foreach ($arr as $key => $value) {
					$str .= intval($value) > 0 ? $da_num[intval($value)] . '室 ' : '别墅 ';
				}
			}
			$data->layout = trim($str) ? trim($str) : '商住户型';
		}
		

		if($row && $row -> save((array)$data)) {
		 	//推荐id插入推荐表
		 	$arr = new \stdClass();
		 	$arr->table_id = self::getTableId();
			$arr->list_id  = $row->id;
			$arr->reco_ids = $data->reco_ids;
		 	RecoService::saveRecoList($arr);
	    }		
	}

	/**
	 * 2017-3-28 +
	 * [batchReco 批量 推荐/批量删除 到 推荐列表  首页列表]
	 * @param  [type]  $id_arr [操作的id 数组形式]
	 * @param  integer $action [2推荐到推荐列表 1推荐到首页列表 默认取消推荐]
	 * @return [type]          [description]
	 */
	public static function batchReco($id_arr = [], $action = FALSE )
	{
		//查询最大的order_by
		$max = EwHouseSeco::maximum(['column' => 'order_by']);
		$reco_by  = 0;
		$index_by = 0;
		switch ($action) {
			case 2://推荐到推荐列表
				$reco_by  = 2;
				$index_by = 0;
				$order_by = $max + 1;
				break;

			case 1://推荐到首页列表
				$reco_by  = 0;
				$index_by = 2;
				$order_by = $max + 1;
				break;

			default://取消推荐
				$reco_by  = 0;
				$index_by = 0;
				$order_by = 100;
				break;
		}

		if ($id_arr && is_array($id_arr) && count($id_arr) > 0){
			foreach ($id_arr as $id) {
				$row = EwHouseSeco::findFirst($id);
				$row->reco_by  = $reco_by;
				$row->index_by = $index_by;
				$row->order_by = $order_by;
				$row->save();
			}
		}
	}

	/**
	 * [houseReco 推荐到为您推荐]
	 * @param  [type] $id_arr [description]
	 * @return [type]         [description]
	 */
	public static function houseReco($id_arr = [])
	{
		foreach ($id_arr as $id) {
			$info = EwHouseSeco::findFirst($id);
			if ($info){
				$arr = new \stdClass;
				// $arr->id = $info->id;
				$arr->list_id = $info->id;
				$arr->house_type = 'ershoufang';
				$arr->three_url = $info->three_url;
				$arr->name = $info->name;
				$arr->tags = $info->tags;
				$arr->house_tags = $info->house_tags;
				$arr->city_id = $info->city_id;
				$arr->region = $info->region;
				$arr->cate_id = $info->cate_id;
				$arr->thumb = $info->thumb;
				$arr->pics = $info->pics;
				$arr->price = $info->price;
				$arr->unit = '元/平';
				$arr->total_price = $info->total_price;
				$arr->area = $info->area;
				$arr->address = $info->address;
				$arr->property = $info->property;
				$arr->layout = $info->layout;
				$arr->house_name = $info->house_name;
				$arr->house_orien = $info->house_orien;
				// $arr->rent_type = '';
				$arr->lon = $info->lon;
				$arr->lat = $info->lat;
				$arr->sources = $info->sources;
				$arr->comment = $info->comment;
				$arr->visit = $info->visit;
				$arr->is_self = $info->is_self;
				$arr->is_stick = TRUE;//推荐自动置顶
				// $arr->is_display = 1;
				// $arr->is_del = 0;
				// $arr->order_by = 100;
				// $arr->reco_by = '0';
				// $arr->index_by = '0';
				// $arr->create_time = $info->create_time;
				// $arr->update_time = $info->update_time;
				HouseRecoService::save($arr);
				$info->is_reco_you = 1;
			    $info->save();
			}
		}
	}
}